<?php
	require_once('../config.php');
	include_once("../database/DataBase.php");
	include_once("../include/function.php");
				
	session_start();
	checkLogin();

	$cid = $_SESSION["company_id"];
	$pid = $_GET["pid"];
	$database = New DataBase();
	$database->open_connection();
	$category = $database->getProductCategory($cid, $_SESSION["language"]);
	$status = $database->getStatus();
	
	$result = $database->getProductByPID($pid);
	$product = mysql_fetch_assoc($result);
	
	$result_image = $database->getProductImageByPID($pid);
	$result_template = $database->getProductTemplateByPID($pid);
	
?>

<html>
	<head>
		<title>CMS - Item</title>
		<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
		<?php
			include_once("../include/includeHeader.php");
		?>
		<link rel="Stylesheet" type="text/css" href="<?echo $config["root"];?>/css/jHtmlArea.css" />
		<script type="text/javascript" src="<?echo $config["root"];?>/js/jHtmlArea"></script>
		<script type="text/javascript">
		
			$(document).ready(function(){
				$('.p_desc').htmlarea();
			});
		
			$(function() {
				$( "#p_storein" ).datepicker();
				$( "#p_expire" ).datepicker();
			});
		
			function checkFileSize(object){
				alert(object.files[0].size);
			}
			
			function checkSubmit(){
				var message;
				var count;

				message = "";
				count = 0;
				$('.p_name').each(function(){
					if ($(this).val() != ""){
						count++;
					}
				});
				if(count == 0){
					$(".p_name").addClass("error");
					message = message+ "Please input Product name\n";
				}
				
				if($("#p_cost").val() ==  ""){
					$("#p_cost").addClass("error");
					message = message+ "Please input product cost\n";
				}
				else if(isNaN($("#p_cost").val())){
					$("#p_cost").addClass("error");
					message = message+ "Please input correct product cost\n";
				}
				else{
					$("#p_cost").removeClass("error");
				}
								
				if($("#p_price").val() ==  ""){
					$("#p_price").addClass("error");
					message = message+ "Please input product price\n";
				}
				else if(isNaN($("#p_price").val())){
					$("#p_price").addClass("error");
					message = message+ "Please input correct product price\n";
				}
				else{
					$("#p_price").removeClass("error");
				}
				
				if($("#p_quantity").val() ==  ""){
					$("#p_quantity").addClass("error");
					message = message+ "Please input product quantity\n";
				}
				else if(isNaN($("#p_quantity").val())){
					$("#p_quantity").addClass("error");
					message = message+ "Please input correct product quantity\n";
				}
				else{
					$("#p_quantity").removeClass("error");
				}
				
				/*
				$(".p_image").each(function(){
					if ($(this).val() != ""){
						if(document.getElementById("Image1").files[0].size > 2000000){
							$(this).addClass("error");
							message = message+ $(this).attr('id')+" error, Max file size is 2MB\n";
						}
						else{
							$(this).removeClass("error");
						}
					}
				});
				*/
				if(message != ""){
					alert(message);
				}
				else{
					document.new_product.submit();
				}
			}
		</script>
	</head>
	<body>
		<?php
			include_once("../include/top_navigation.php");
		?>
		<div Class="Middle">
			<div class="LeftMenu">
				<?php include_once("../include/left_menu.php"); ?>
			</div>
			<div id="RightContent" >
				<form name="new_product" method="POST" action="../include/product_edit_process.php" enctype="multipart/form-data">
				<table>
					<tr>
						<td>Product Code : </td>
						<td><input type="text" name="p_code" class="p_code" id="p_code" value="<? echo $product["product_code"];?>" /> </td>
					</tr>
					<? 
						
						$i = 0;
						while ($row = mysql_fetch_assoc($result_template)) {
					?>
					<tr>
						<td>Name : </td>
						<td><input type="text" name="p_name[]" class="p_name" id="p_name" value="<? echo $row["name"];?>" /> </td>
					</tr>
					<tr>
						<td>Description : </td>
						<td>
							<textarea name="p_desc[]" id="p_desc" rows="10" class="p_desc" cols="100"><? echo $row["description"];?></textarea>
						</td>
					</tr>
					<? 
						$i++;
						} 
					?>
					
					<? 
						if ($i < 2){ 
						for($j=$i; $j<3;$j++){
					?>
					<tr>
						<td>Name : </td>
						<td><input type="text" name="p_name[]" class="p_name" id="p_name" value="" /> </td>
					</tr>
					<tr>
						<td>Description : </td>
						<td>
							<textarea name="p_desc[]" id="p_desc" rows="10" class="p_desc" cols="100"></textarea>
						</td>
					</tr>	
					<?
						}
						} 
					?>
					<tr>
						<td>Cost : </td>
						<td><input type="text" name="p_cost" id="p_cost" value="<? echo $product["cost"];?>" /></td>
					</tr>
					<tr>
						<td>Selling Price : </td>
						<td><input type="text" name="p_price" id="p_price" value="<? echo $product["price"];?>" /></td>
					</tr>
					<tr>
						<td>Quantity : </td>
						<td><input type="text" name="p_quantity" id="p_quantity" value="<? echo $product["quantity"];?>" /></td>
					</tr>
					<tr>
						<td>Category : </td>
						<td>
							<select name="p_category" id="p_category">
								<option> ---- </option>
								<?php
									while ($row = mysql_fetch_assoc($category)) {
										if($product["category_id"] == $row["category_id"]){
											echo "<option value='".$row["category_id"]."' selected>".$row["name"]."</option>";
										}
										else{
											echo "<option value='".$row["category_id"]."'>".$row["name"]."</option>";
										}
									}
								?>
							</select>
						</td>
					</tr>
					<tr>
						<td>Store in Date : </td>
						<td><input type="text" name="p_storein" id="p_storein" value="<? echo $product["store_in_date"];?>" placeholder="DD/MM/YYY" /></td>
					</tr>
					<tr>
						<td>Expire Date : </td>
						<td><input type="text" name="p_expire" id="p_expire" value="<? echo $product["expire_date"];?>" placeholder="DD/MM/YYY" /></td>
					</tr>
					
					<tr>
						<td>Status : </td>
						<td>
							<select name="p_status" id="p_status">
								<option> ---- </option>
								<?php
									while ($row = mysql_fetch_assoc($status)) {
										if($product["status_id"] == $row["status_id"]){
											echo "<option value='".$row["status_id"]."' selected>".$row["status"]."</option>";
										}
										else{
											echo "<option value='".$row["status_id"]."'>".$row["status"]."</option>";
										}
									}
								?>
							</select>
						</td>
					</tr>
					
					<tr>
						<td valign="top">Image : </td>
						<td>
							<p>
								<?
									while ($row = mysql_fetch_assoc($result_image)) {
										echo "<img src='".$row["image_url"]."' width='10%' height='10%' border='0' />";
									}
								?>
							</p>
							<p>Image1 :<input type="file" name="p_image[]" id="Image1" class="p_image" /></p>
							<p>Image2 :<input type="file" name="p_image[]" id="Image2" class="p_image" /></p>
							<p>Image3 :<input type="file" name="p_image[]" id="Image3" class="p_image" /></p>
						</td>
					</tr>
					<tr>
						<td colspan="2"><input type="button" name="button" value="Submit" onclick="javascript: checkSubmit();"/></td>
					</tr>
				</table>
				<input type="hidden" name="pid" id="pid" value="<? echo $pid;?>" />
				<input type="hidden" name="cid" id="cid" value="<? echo $cid;?>" />
				</form>
			</div>
		</div>
		<?php
			include_once("../include/footer.php");
		?>
	</body>
</html>
<?
	mysql_free_result($status);
	mysql_free_result($category);
	$database->close_connection();
?>